package com.yx.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.common.utils.JwtUtil;
import com.yx.common.vo.Result;
import com.yx.sys.entity.Post;
import com.yx.sys.entity.User;
import com.yx.sys.mapper.PostMapper;
import com.yx.sys.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 提问或推荐 服务实现类
 * </p>
 *
 * @author yx
 * @since 2023-02-14
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
    @Autowired(required = false)
    private PostMapper postMapper;

    @Autowired
    private JwtUtil jwtUtil;
//    根据tId查询post,user,tags信息
    @Override
    public List<Post> getPostMesBytId(Integer tId) {
        List<Post> list = postMapper.selectPostBytId(tId);
        return list;
    }

    @Override
    public List<?> getFuzzyTitle(String titleName) {
        List<?> list = postMapper.selectFuzzyTitle(titleName);

        return list;
    }

    @Override
    public List<Post> getNewPosts(Integer num) {
        List<Post> list = postMapper.selectNewPosts(num);
        return list;
    }

    @Override
    public List<Post> getHotPostsBytId(Integer tId,Integer num) {
        List<Post> list = postMapper.selectHotPostsBytId(tId,num);
        return list;
    }

    @Override
    public List<Post> getHotPosts(Integer num) {
        List<Post> list = postMapper.selectHotPosts(num);
        return list;
    }

    @Override
    public List<Post> getPostMesByuId(Integer uId) {
        List<Post> list = postMapper.selectPostByuId(uId);
        return list;
    }

    @Override
//    public List<Post> getMesByuId(Integer status,Integer uId,Integer startId,Integer pageSize,Integer tag) {
    public List<Post> getMesByuId(Integer status,Integer uId,Integer tag) {
//        User loginUser = null;
//        try{
//            loginUser = jwtUtil.parseToken(token,User.class);
//        }catch(Exception e){
//            e.printStackTrace();
//        }

//        if(loginUser!=null){
//            QueryWrapper<Post> wrapper = new QueryWrapper<>();
//            wrapper.eq("uId",loginUser.getuId());
//            List<Post> list = postMapper.selectList(wrapper);
//            List<Post> listResult = postMapper.selectPostMesByuId(status,uId,startId,pageSize,tag);
        List<Post> listResult = postMapper.selectPostMesByuId(status,uId,tag);
        return listResult;
//        }
//        return null;
    }

    @Override
    public Integer getPostIsExist(String titleName, String pImg, String pDescribe, Integer tId, Integer uId) {
        Integer pId = postMapper.selectPostIsExist(titleName, pImg, pDescribe, tId, uId);
        return pId;
    }

    @Override
    public List<Post> getPostBypId(Integer pId) {
        List<Post> posts = postMapper.selectPostBypId(pId);
        return posts;
    }
}
